from django import forms


class RegisterForm(forms.Form):
    # username相当于html中的'name'属性
    username = forms.CharField(label='用户名', max_length=20)  # label相当于html中form表单中的label标签
    password = forms.CharField(label='密码',                   # '密码'相当于input标签前面显示的文字
                               max_length=8,
                               min_length=6,                  # PasswordInput相当于input标签，placeholder相当于input的属性
                               widget=forms.PasswordInput(attrs={'placeholder': '请输入长度6-8的密码'}),
                               error_messages={'min_length': '密码长度小于6',  # 加样式通过attrs去加
                                               'max_length': '密码长度大于8',
                                               })
    password_repeat = forms.CharField(label='请再次输入密码', widget=forms.PasswordInput())
    email = forms.EmailField()

    def clean(self):
        cleaned_data = super().clean()

        password = cleaned_data.get('password')
        password_repeat = cleaned_data.get('password_repeat')

        if password_repeat != password:
            self.add_error('password_repeat', '密码不一致')
